package org.example.controller;

/**
 * @BelongsProject: Dynamic-relationship-binding
 * @BelongsPackage: org.example.controller
 * @Author: dengxiyan
 * @CreateTime: 2025/07/04  11:05
 * @Description: TODO
 * @Version: 1.0
 */

import org.example.model.Course;
import org.example.model.Student;
import org.example.model.Teacher;
import org.example.service.CourseService;
import org.example.service.StudentService;
import org.example.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/entities")
public class EntityController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

    // 学生相关接口
    @GetMapping("/students")
    public Page<Student> getAllStudents(Pageable pageable) {
        return studentService.findAll(pageable);
    }

    @PostMapping("/students")
    public Student createStudent(@RequestBody Student student) {
        return studentService.save(student);
    }

    @PostMapping("/students/{id}/add-course/{courseId}")
    public Student addCourseToStudent(@PathVariable Long id, @PathVariable Long courseId) {
        return studentService.addRelation(id, courseId, "courses");
    }

    @GetMapping("/students/{id}/courses")
    public Page<Object> getStudentCourses(@PathVariable Long id, Pageable pageable) {
        return studentService.findRelatedEntities(id, "courses", pageable);
    }

    // 教师相关接口
    @GetMapping("/teachers")
    public Page<Teacher> getAllTeachers(Pageable pageable) {
        return teacherService.findAll(pageable);
    }

    @PostMapping("/teachers/{id}/add-student/{studentId}")
    public Teacher addStudentToTeacher(@PathVariable Long id, @PathVariable Long studentId) {
        return teacherService.addRelation(id, studentId, "students");
    }
    @PostMapping("/teachers")
    public Teacher createTeacher(@RequestBody Teacher teacher) {
        return teacherService.save(teacher);
    }

    @GetMapping("/teachers/{id}/students")
    public Page<Object> getTeacherStudents(@PathVariable Long id, Pageable pageable) {
        return teacherService.findRelatedEntities(id, "students", pageable);
    }

    // 课程相关接口
    @GetMapping("/courses")
    public Page<Course> getAllCourses(Pageable pageable) {
        return courseService.findAll(pageable);
    }

    @PostMapping("/courses")
    public Course createTeacher(@RequestBody Course course) {
        return courseService.save(course);
    }
}
